package net.j4love.spring.mvc.ex.mapping;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author He Peng
 * @create 2017-06-09 14:29
 * @update 2017-06-09 14:29
 * @updatedesc : 更新说明
 * @see
 */

@Controller
@RequestMapping("/mapping")
public class RequestMappingController {

    @RequestMapping(
            path = "/path"
    )
    public String pathHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/path");
        model.put("method","GET || POST || PUT || DELETE || PATCH");
        return "show";
    }

    @RequestMapping(
            path = "/get",
            method = RequestMethod.GET
    )
    public String getHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/get");
        model.put("method","GET");
        return "show";
    }

    @RequestMapping(
            path = "/post",
            method = RequestMethod.POST
    )
    public String postHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/post");
        model.put("method","POST");
        return "show";
    }

    @RequestMapping(
            path = "/put",
            method = RequestMethod.PUT
    )
    public String putHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/put");
        model.put("method","PUT");
        return "show";
    }

    @RequestMapping(
            path = "/delete",
            method = RequestMethod.DELETE
    )
    public String deleteHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/delete");
        model.put("method","DELETE");
        return "show";
    }

    @RequestMapping(
            path = "/headers",
            headers = "YOURNAME"
    )
    public String headerHandler(Map<String,Object> model,
                                         HttpServletRequest request) {
        model.put("handler",this.getClass().getName());
        model.put("path","/headers");
        model.put("header",request.getHeader("YOURNAME"));
        return "show";
    }

    @RequestMapping(
            path = "/consumes",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
    )
    public String consumesHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/consumes");
        model.put("consumes",MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        return "show";
    }

    @RequestMapping(
            path = "/produces",
            produces = MediaType.APPLICATION_JSON_VALUE
    )
    public String producesHandler(Map<String,Object> model) {
        model.put("handler",this.getClass().getName());
        model.put("path","/produces");
        model.put("produces",MediaType.APPLICATION_JSON_VALUE);
        return "show";
    }
}
